package com.monitor.test;

import com.monitor.entity.pojo.user.User;
import com.monitor.entity.response.Rs;
import com.monitor.utils.PoJoUtil;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;

/**
 * .
 *
 * @author Li Zhiyuan
 * @since 2020-11-27
 */
public class testRestFul {
    @Test
    public void restTemplateGetTest(){
        RestTemplate restTemplate=new RestTemplate();
        //--------------------带header请求头参数的get请求
        HttpHeaders headers=new HttpHeaders();
        headers.add("token","54CBF01E9EDA5C928D3D9265F9DAFED9");
        HttpEntity<String> re=new HttpEntity<>(null,headers);
        restTemplate.exchange("http://127.0.0.1:8080/sediment", HttpMethod.GET,re,Object.class);
        //--------------------不带请求头参数的get请求
        Object o=restTemplate.getForObject("http://127.0.0.1:8080/sediment",Object.class);
        System.out.println(o);
    }

    //用户登录接口
    @Test
    public void restTemplatePostTest() throws UnsupportedEncodingException, NoSuchFieldException {
        RestTemplate restTemplate=new RestTemplate();
        String url="http://127.0.0.1:8080/user/login";
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String,String> map=new LinkedMultiValueMap<>();
        map.add("userName","csdn1");
        map.add("passWord","1");

        HttpEntity<MultiValueMap<String,String>> request=new HttpEntity<>(map,headers);
//        Object o=restTemplate.postForEntity(url,request,Object.class);
        ResponseEntity<Rs> o=restTemplate.postForEntity(url,request,Rs.class);
        LinkedHashMap user = (LinkedHashMap) o.getBody().getData();
        String token= (String) user.get("token");
        System.out.println(token);
    }
    @Resource
    private RedisTemplate<String,User> redisTemplate;

    @Test
    public void redis(){
        redisTemplate.opsForHash().put("csdn1","cs","csdn");
        redisTemplate.opsForHash().values("csdn1");
        redisTemplate.opsForHash().keys("8EBBA42E0DF137AA23B3389BC91529BB");
    }

}
